package com.example.interesting.distributed.log.realize.feign;

import com.example.interesting.distributed.log.realize.util.LogTraceInit;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.core.Ordered;

import java.util.Map;

/**
 * @Description: 基于feign调用的请求拦截处理请求头信息
 * @Author: fyw
 * @Date: 2024/3/30 16:08
 * @History
 */
public class LogFeignInterceptor implements RequestInterceptor, Ordered {

    @Override
    public void apply(RequestTemplate requestTemplate) {
        //获取mdc日志扩展信息，并替换服务名
        Map<String, String> stringStringMap = LogTraceInit.replaceServiceName();
        //添加到请求头传递到下面的服务
        stringStringMap.forEach(requestTemplate::header);
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE;
    }
}
